//
//  OnboardingVM.swift
//  Kosspot
//
//  Created by 胡辉 on 2025/7/11.
//

import RxSwift
import Combine
import ToastUI
import SwiftUI
import Moya


class OnboardingVM: ObservableObject {
    @Published var gender: Gender = .male
    @Published var birthday = Date()
    @Published var weight: Double = 80
    @Published var height: Double = 180
    @Published var steps: Double = 5000
    
    @Published var weightUnit: WeightUnit = .kg
    @Published var heightUnit: HeightUnit = .cm
    
    @Published var error: String?
    
    private let disposeBag = DisposeBag()
    
    
    
    
    func updateUserInfo(completion: @escaping (Result<Void, Error>) -> Void) {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        var birthdayStr = formatter.string(from: birthday)
//        NetworkManager.shared.updateUserInfo(
//            nickname: "用户167",
//            gender: gender.intRaw,
//            birthday: birthdayStr,
//            height: String(format: "%.1f", height),
//            weight: String(format: "%.1f", weight)
//        )
//        .observe(on: MainScheduler.instance)
//        .subscribe(
//            onSuccess: { [weak self] response in
//                self?.error = nil
//                completion(.success(()))
//            },
//            onFailure: { [weak self] error in
//                self?.error = error.localizedDescription
//                completion(.failure(error))
//            }
//        )
//        .disposed(by: disposeBag)
        NetworkManager.shared.request(
            MultiTarget(
                ApiService.updateUserInfo(
                    nickname: "用户167",
                    gender: gender.intRaw,
                    birthday: birthdayStr,
                    height: String(format: "%.1f", height), 
                    weight: String(format: "%.1f", weight)
                )
            ), 
            responseType: SimpleResponse.self
        )
        .observe(on: MainScheduler.instance)
        .subscribe(
            onNext: { [weak self] response in
                self?.error = nil
                completion(.success(()))
            },
            onError: { [weak self] error in
                self?.handleError(error)
                completion(.failure(error))
            }
        )
        .disposed(by: disposeBag)
    }
    
    private func handleError(_ error: Error) {
        if let networkError = error as? NetworkError {
            self.error = networkError.errorDescription
//            if networkError.isAuthenticationError {
//                AppState.shared.logout()
//            }
        } else {
            self.error = error.localizedDescription
        }
    }
    
}

enum Gender: String, CaseIterable {
    case male = "male"
    case female = "female"
    
    var intRaw: Int {
        switch self {
        case .male: return 1
        case .female: return 2
        }
    }
    
    var title: String {
        switch self {
        case .male: return "我是男生"
        case .female: return "我是女生"
        }
    }
    var defaultImage: String {
        switch self {
        case .male: return "male_unselected"
        case .female: return "female_unselected"
        }
    }
    
    var selectedImage: String {
        switch self {
        case .male: return "male_selected"
        case .female: return "female_selected"
        }
    }
    
    func imageName(isSelected: Bool) -> String {
            return isSelected ? selectedImage : defaultImage
        }
}

enum WeightUnit: String, CaseIterable {
    case g = "g"
    case kg = "kg"
    
    var title: String {
        switch self {
        case .g: return "g"
        case .kg: return "kg"
        }
    }

    func bgColor(isSelected: Bool) -> Color {
            return isSelected ? mainColor : Color(hex: "#333333")
        }
    func textColor(isSelected: Bool) -> Color {
            return isSelected ? Color(hex: "#242424") : Color(hex: "#F5F5F5")
        }
    
}

enum HeightUnit: String, CaseIterable {
    case cm = "cm"
    case m = "m"
    
    var title: String {
        switch self {
        case .cm: return "cm"
        case .m: return "m"
        }
    }

    func bgColor(isSelected: Bool) -> Color {
            return isSelected ? mainColor : Color(hex: "#333333")
        }
    func textColor(isSelected: Bool) -> Color {
            return isSelected ? Color(hex: "#242424") : Color(hex: "#F5F5F5")
        }
    
}
